/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 18/07/2004
 *
 */

package au.com.lastweekend.jim.io.jpeg;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.GregorianCalendar;

import junit.framework.TestCase;
import au.com.lastweekend.jim.io.exif.ExifDirectory;
import au.com.lastweekend.jim.io.metadata.Metadata;
import au.com.lastweekend.jim.util.LoggingOutputStream;

/**
 * Base class for IO tests
 * 
 * @version $Id: JimIOTestCase.java,v 1.2 2005/06/09 11:15:36 ggardner Exp $
 * @author grant@lastweekend.com.au
 * 
 */
public abstract class JimIOTestCase extends TestCase {

    public static final String DATE_AS_EXIF_DESCRIPTION = "1971:06:11 14:30:59";
    public static final Date MODIFIED_DATE = (new GregorianCalendar(1971, 05, 11, 14, 30, 59)).getTime();

    protected OutputStream getLoggingOutputStream(File jpegFile) throws JpegProcessingException {

        try {
            OutputStream outStream = new FileOutputStream(jpegFile);
            return new LoggingOutputStream(new BufferedOutputStream(outStream));
        } catch (FileNotFoundException e) {
            throw new JpegProcessingException(e);
        }

    }

    protected Metadata readImage(File imageFile) throws JpegProcessingException {

        JpegReader reader = new JpegReader();
        Metadata metadata = reader.readImage(imageFile);
        return metadata;
    }

    /**
     * @throws JpegProcessingException
     * @throws IOException
     */
    protected void readWriteImage(File inputFile, File outputFile) throws JpegProcessingException, IOException {

        Metadata metadata = readImage(inputFile);
        ExifDirectory exifDir = (ExifDirectory) metadata.getDirectory(ExifDirectory.class);
        exifDir.setDateTime(ExifDirectory.TAG_DATETIME_ORIGINAL, MODIFIED_DATE);
        JpegWriter writer = new JpegWriter();
        OutputStream output = getLoggingOutputStream(outputFile);
        writer.writeImage(metadata, output);
        output.close();
    }

}